<template>
  <el-dialog
    append-to-body
    :close-on-click-modal="false"
    :visible.sync="dialogFormVisible"
    :before-close="close"
    :title="title"
    width="800px"
    :top="'10vh'"
  >
    <el-form ref="form" :model="form" size="small" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item
            label="客户端名称"
            prop="clientName"
            :rules="{ required: true, message: '模版名称不能为空', trigger: 'blur' }"
            style="width: 100%"
          >
            <el-input v-model="form.clientName" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="客户端密钥"
            prop="clientSecret"
            :rules="{ required: true, message: '客户端密钥不能为空！', trigger: 'blur' }"
            style="width: 100%"
          >
            <el-input v-model="form.clientSecret" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="接口权限"
            prop="apiAuth"
            :rules="{ required: true, message: '请选择接口权限！', trigger: 'change' }"
            style="width: 100%"
          >
            <el-select v-model="clientIp" style="width: 100%" multiple placeholder="请选择接口权限">
              <el-option
                v-for="item in dataList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="ip白名单"
            prop="clientIp"
            :rules="{ required: true, message: '请设置ip白名单！', trigger: 'blur' }"
            style="width: 100%"
          >
            <el-input
              v-model="form.clientIp"
              type="textarea"
              :rows="2"
              placeholder="多个Ip请用‘;’号隔开"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="是否启用" prop="zt" :rules="{ required: true, message: '请选择接是否启用！', trigger: 'change' }">
            <el-radio-group v-model="form.zt" style="width: 200px">
              <el-radio
                v-for="item in status"
                :key="item.id"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="close">取消</el-button>
      <el-button :loading="loading" type="primary" @click="save('form')">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { queryAllNoticeConstant, saveEntity, updateEntity } from '@/api/webhook/config/index';
export default {
  name: 'NoticeTemplateManagerEntityIndex',
  data() {
    return {
      loading: false,
      dialogFormVisible: false,
      title: '新增',
      status: [{ id: 0, value: false, label: '否' },
        { id: 1, value: true, label: '是' }],
      clientIp: [],
      form: {
        id: null,
        clientName: '',
        clientKey: '',
        clientIp: '',
        clientSecret: '',
        zt: true,
        apiAuth: ''
      },
      dataList: []
    };
  },
  watch: {
    clientIp: {
      handler(newName, oldName) { // 值发生变化时
        if (newName !== null && newName.length > 0) {
          this.form.apiAuth = newName.toString();
        } else {
          this.form.apiAuth = '';
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  created() {
    this.initData();
  },
  methods: {
    openEntity(data) {
      this.clientIp = [];
      this.bqList = [];
      if (undefined === data || data === null || data.id === undefined || data.id === null) {
        this.title = '新增';
        this.dialogFormVisible = true;
      } else {
        this.title = '编辑';
        this.dialogFormVisible = true;
      }
      this.form = Object.assign({}, data);
      if (undefined !== this.form.apiAuth && this.form.apiAuth !== null && this.form.apiAuth !== '') {
        this.clientIp = this.form.apiAuth.split(',').map(item => {
          return parseInt(item);
        });
      }
    },
    save(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.form.id !== null && undefined !== this.form.id) {
            updateEntity(this.form).then(res => {
              this.$message.success('更新成功！');
              this.close();
              this.loading = false;
            }).catch(() => {
              this.$message.error('更新失败！');
              this.loading = false;
            });
          } else {
            saveEntity(this.form).then(res => {
              this.$message.success('保存成功！');
              this.loading = false;
              this.close();
            }).catch(() => {
              this.loading = false;
              this.$message.error('保存失败！');
            });
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    initData() {
      queryAllNoticeConstant().then(res => {
        const { data } = res;
        if (undefined !== data && data !== null) {
          this.dataList = data.data;
        }
      }).catch(() => {
        console.log('获取数据字典信息失败！');
      });
    },
    close() {
      Object.assign(this.$data.form, this.$options.data.call(this).form);
      this.dialogFormVisible = false;
      this.$emit('fetch-data');
    }
  }
};
</script>

<style scoped>

</style>
